<?php
/***
 * 碎片质押
 */
namespace app\api\controller;

use app\api\controller\BaseController;

class PledgeController extends BaseController{
    /*
    **创建碎片质押
    */
	public function createOrder(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $token			= request()->param('token','','trim') ;
        $debri_id			= request()->param('debri_id',0,'intval') ;

        if(!$token){
            return ['code'=>0,'code_dec'=>'用户信息不存在'];
        }
        $data['uid']=0;
		if($token){
			$userArr = explode(',',auth_code($token,'DECODE'));

			$data['uid'] = $userArr[0];
		}
        if(!$debri_id){
            return json(['code'=>0,'data'=>[],'code_dec'=>'碎片id必传']);
        }
        $info=db('users_goods')->where(['status'=>1,'uid'=>$data['uid'],'id'=>$debri_id])->find();
        if($info['total_number']<=0 || $info['available_number']<=0){
            return json(['code'=>0,'data'=>[],'code_dec'=>'碎片可用数量不足']);
        }
        $data['number'] = request()->param('number',0,'trim') ;
        if( $data['number']<=0){
            return json(['code'=>0,'data'=>[],'code_dec'=>'质押碎片数量不能小于1']);
        }
        $data['type_id'] = request()->param('type_id',0,'intval') ;
        $typeInfo=db('pledge_tax')->where(['status'=>1,'id'=>$data['type_id']])->find();
        if(empty($typeInfo)){
            return json(['code'=>0,'data'=>[],'code_dec'=>'质押类型不存在']);
        }
        if(($typeInfo['min']>0 && $data['number'] <$typeInfo['min'] ) || ($typeInfo['max']>0 && $data['number'] >$typeInfo['max'] )){
            return json(['code'=>0,'data'=>[],'code_dec'=>'质押碎片数量范围错误']);
        }
        if($data['number']>$info['available_number']){
            return json(['code'=>0,'data'=>[],'code_dec'=>'可用押碎片数量不足']);
        }

        $time=time();
        $data['create_time'] = $time ;
        $data['nft_id'] = $info['nft_id'] ;
        $data['goods_id'] =  $debri_id ;
        $data['award'] = $typeInfo['money'];
        $data['total_award'] = $typeInfo['money']*$data['number'] -  $typeInfo['fee'];
        $data['fee'] = $typeInfo['fee'];
        $data['day'] = $typeInfo['day'];
        $data['end_time'] = $time + $typeInfo['day']*3600*24;
        $data['status']=1;
        $data['order_sn'] ='P'.date('YmdHis').rand(0,9).rand(0,9).rand(0,9).rand(0,9) ;
		$res = db('users_pledge_orders')->insert($data);

        //将可用碎片减去
        db('users_goods')->where(['id'=>$debri_id])->Dec('available_number',$data['number'])->update();
		return json(['code'=>1,'data'=>[],'code_dec'=>config($lang.'.lang2')[3]]);
    }
    /*
    **列表
    */
	public function getList(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $param		= input('post.');
        $token			= (input('post.token')) ? input('post.token') : '';
		$where = 'a.status !=-1';
		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			$uid		= $userArr[0];
			$where.=" and a.uid=$uid";

		}
        if(isset($param['status']) && !empty($param['status'])){
			$where.=" and a.status = {$param['status']}";
		}
        if(isset($param['nft_id']) && !empty($param['nft_id'])){
			$where.=" and a.nft_id = {$param['nft_id']}";
		}
        if(isset($param['debri_id']) && !empty($param['debri_id'])){
			$where.=" and a.goods_id = {$param['debri_id']}";
		}

		//每页记录数
		$pageSize	= (isset($param['pagesize']) and $param['pagesize']) ? $param['pagesize'] : 10;
		//当前页
		$page		= (isset($param['page']) and $param['page']) ? $param['page'] : 1;

        $data	= db('users_pledge_orders')->alias('a')
            ->field('a.*,b.name,b.file,b.credit_type,b.characteristic,b.content,b.reward_price,b.recovery_price,b.release_price')
            ->join('nft b','a.nft_id=b.id','left')
            ->where($where)
            ->order('a.create_time desc')
            ->paginate($pageSize)
            ->toArray();

        $statusArr=config($lang.'.status');
		foreach ($data['data'] as $key => &$value) {
			$value['file']=$value['file']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$value['file']):'';
            $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
            $value['end_time']=date('Y-m-d H:i:s',$value['end_time']);
            $value['statusStr']=$statusArr[$value['status']];
            $value['credit_type_str']= getCreditTypeName($value['credit_type']);
		}
		return json(['code'=>1,'data'=>$data]);
    }
     /*
    **详情
    */
	public function getDetail(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $id		= input('post.id');
        if(empty($id)){
            return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[7]]);
        }
        $token			= (input('post.token')) ? input('post.token') : '';
		$where = 'a.status !=-1 and a.id='.$id;
		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			$uid		= $userArr[0];
			$where.=" and a.uid=$uid";

		}

        $data	= db('users_pledge_orders')->alias('a')
            ->field('a.*,b.name,b.file,b.credit_type,b.characteristic,b.content,b.reward_price,b.recovery_price,b.release_price')
            ->join('nft b','a.nft_id=b.id','left')
            ->where($where)
            ->find();
        if(empty($data)){
            return ['code'=>0,'code_dec'=>config($lang.'.lang2')[8]];
        }
        $statusArr= config($lang.'.status');
		$data['file']=$data['file']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['file']):'';
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        $data['statusStr']=$statusArr[$data['status']];
        $data['credit_type_str']= getCreditTypeName($data['credit_type']);

		return json(['code'=>1,'data'=>$data]);
    }
    /*
    **撤回质押
    */
	public function  revoke(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $id		= input('post.id');
        if(empty($id)){
            return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[7]]);
        }
        $token			= (input('post.token')) ? input('post.token') : '';
		$where = 'a.status =1 and a.id='.$id;
		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			$uid		= $userArr[0];
			$where.=" and a.uid=$uid";

		}

        $data	= db('users_pledge_orders')->alias('a')
            ->field('a.*,b.name,b.file,b.credit_type,b.characteristic,b.content,b.reward_price,b.recovery_price,b.release_price')
            ->join('nft b','a.nft_id=b.id','left')
            ->where($where)
            ->find();
        if(empty($data)){
            return ['code'=>0,'code_dec'=>config($lang.'.lang2')[8]];
        }

        db('users_pledge_orders')->where(['id'=>$id])->update(['status'=>3,'revoke_time'=>time()]);

        //把碎片可用数量重新计算
        db('users_goods')->where('id',$data['goods_id'])->Inc('available_number',$data['number'])->update();
		return json(['code'=>1,'data'=>[],'code_dec'=>config($lang.'.lang2')[3]]);
    }

    /**
	 * Undocumented function
	 *
	 * @return void
	 */
	public function getStatus($lang){
		return [
			1=>'进行中',
			2=>'赎回成功',
            3=>'已撤回',
		];
	}

}
